package com.citycloud.ccuap.tc.oauth2.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.citycloud.ccuap.tc.oauth2.config.CustomerProperties;
import com.citycloud.ccuap.tc.oauth2.dto.SiteInfoForSwitchSite;
import com.citycloud.ccuap.tc.oauth2.dto.SiteType;
import com.citycloud.ccuap.tc.oauth2.dto.UserInfoForSwitchSite;
import com.citycloud.ccuap.tc.oauth2.dto.UserInfoSites;
import com.citycloud.ccuap.tc.oauth2.mapper.ProjectMapper;
import com.citycloud.ccuap.tc.oauth2.service.SiteService;

@Service
public class SiteServiceImpl implements SiteService {
	@Resource
	private ProjectMapper sysprojectMapper;

	@Override
	public String getClientIdBySiteId(Long siteId) {
		return sysprojectMapper.selectClientIdBySiteId(siteId);
	}

	@Override
	public List<SiteInfoForSwitchSite> selectListByUserId(Long userId, String projectTypeId, Boolean appType) {
		return sysprojectMapper.selectListByUserId(userId, projectTypeId, appType);
	}

	@Override
	public List<SiteType> selectSiteTypes() {
		return sysprojectMapper.selectSiteTypes();
	}

	@Override
	public UserInfoSites getSwitchSiteList(Long userId, String projectTypeId, Boolean appType) {
		UserInfoSites userInfoSites = new UserInfoSites();
		// 获取站点类型列表
		List<SiteType> siteTypes = selectSiteTypes();
		if (CollectionUtils.isEmpty(siteTypes)) {
			userInfoSites.setSiteTypes(new ArrayList<>());
			return userInfoSites;
		}
		// 获取用户关联站点列表
		List<SiteInfoForSwitchSite> sites = selectListByUserId(userId, projectTypeId, appType);
		if (CollectionUtils.isEmpty(sites)) {
			// 站点列表为空
			userInfoSites.setSiteTypes(new ArrayList<>());
			return userInfoSites;
		}

		Map<String, SiteType> siteTypeMap = new HashMap<>(16);
		siteTypes.forEach(siteType -> {
			siteTypeMap.put(siteType.getId(), siteType);
		});
		List<SiteType> userSiteTypes = new ArrayList<>();
		for (SiteInfoForSwitchSite site : sites) {
			SiteType siteType = siteTypeMap.get(site.getTypeId());
			if (siteType == null) {
                continue;
            }
			List<SiteInfoForSwitchSite> siteInfoList = null;
			if (siteType != null && CollectionUtils.isEmpty(siteType.getSites())) {
				siteInfoList = new ArrayList<>();
			} else {
				siteInfoList = siteType.getSites();
			}
			if (CustomerProperties.authorizationProjectId.equals(site.getProjectId())) {
				UserInfoForSwitchSite userInfoForSwitchSite = new UserInfoForSwitchSite();
				userInfoForSwitchSite.setPlatformSite(site);
				userInfoForSwitchSite.setUserId(userId);
				userInfoSites.setUserInfo(userInfoForSwitchSite);
				continue;
			}
			siteInfoList.add(site);
			siteType.setSites(siteInfoList);
		}
		for (String typeId : siteTypeMap.keySet()) {
			SiteType type = siteTypeMap.get(typeId);
			if (!CollectionUtils.isEmpty(type.getSites())) {
				userSiteTypes.add(type);
			}
		}
		// 排序
		Collections.sort(userSiteTypes, new Comparator<SiteType>() {
			@Override
			public int compare(SiteType o1, SiteType o2) {
				int i = (o1.getSeq() == null ? Integer.MIN_VALUE : o1.getSeq())
						- (o2.getSeq() == null ? Integer.MIN_VALUE : o2.getSeq());
				if (i == 0) {
					return o1.getCreateTime().after(o2.getCreateTime()) ? -1 : 1;
				}
				return i;
			}
		});

		userInfoSites.setSiteTypes(userSiteTypes);
		return userInfoSites;
	}
}
